﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_Default : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dsTemplates.UpdateData += new EventHandler<Acms.DataControls.UpdateDataEventArgs<Template>>(dsTemplates_UpdateData);
        lnkAdd.Click += new EventHandler(lnkAdd_Click);
        gridTemplates.SelectedIndexChanged += new EventHandler(gridTemplates_SelectedIndexChanged);
        gridTemplates.EditCommand += new Telerik.Web.UI.GridCommandEventHandler(gridTemplates_EditCommand);
        gridTemplates.DataBound += new EventHandler(gridTemplates_DataBound);
        if (!IsPostBack)
        {
            if (TemplateId > 0)
            {
                this.Title = string.Format("{0} Details", base.CurrentTemplate.Name);
            }
        }
    }

    void gridTemplates_EditCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        var templateId = (int)gridTemplates.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
        TemplateSet(templateId);
    }

    private void TemplateSet(int id)
    {
        if (TemplateId != id)
        {
            this.TemplateId = id;
            this.Title = string.Format("{0} Details", base.CurrentTemplate.Name);
            ucTabs.DataBind();
            ((BaseMasterPage)this.Master).ReBindBreadCrumbs();
        }
    }

    void gridTemplates_DataBound(object sender, EventArgs e)
    {
        if (base.TemplateId > 0)
        {
            int i = 0;
            foreach (Telerik.Web.UI.DataKey tmp in gridTemplates.MasterTableView.DataKeyValues)
            {
                if ((int)tmp["Id"] == base.TemplateId)
                {
                    gridTemplates.Items[i].Selected = true;
                    break;
                }
                i++;
            }
        }
    }

    void gridTemplates_SelectedIndexChanged(object sender, EventArgs e)
    {
        var templateId = (int)gridTemplates.SelectedValue;
        TemplateSet(templateId);
    }

    void lnkAdd_Click(object sender, EventArgs e)
    {
        gridTemplates.MasterTableView.IsItemInserted = true;
        gridTemplates.MasterTableView.Rebind();
    }

    void dsTemplates_UpdateData(object sender, Acms.DataControls.UpdateDataEventArgs<Acms.Bll.Template> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.BeginTransaction();
            e.Item.SiteId = AcmsContext.SiteId;

            e.Item.Update();
            var templateId = e.Item.Id;

            Acms.Bll.TemplateFile tmp = Acms.Bll.TemplateFile.CreateInstance(e.Item, "Default");
            tmp.IsDefault = true;
            tmp.Path = string.Format("~/Templates/{0}/default.aspx", e.Item.Name.Replace(" ", ""));
            tmp.UseTransaction(e.Item.TransactionKey);

            tmp.Update();

            e.Item.CommitTransaction();
            TemplateSet(e.Item.Id);
        }
        else
        {
            e.Item.Update();
        }
    }

    protected void dsTemplates_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Template> e)
    {
        e.SetData(Template.GetAllBySiteId(AcmsContext.SiteId));
    }

}
